package com.meta.func.bo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.meta.nd.annotation.bo.FdDef;
import com.meta.nd.annotation.bo.NdDef;
import com.meta.nd.vo.Fd;
import com.meta.nd.vo.Nd;
import lombok.Data;


import java.util.ArrayList;
import java.util.List;

@Data
@NdDef(value="InitFilterParamNd")
public class InitFilterParam {
    @FdDef("code")
    String code;
    @FdDef("name")
    String name;
    @FdDef("description")
    String description;
    @FdDef("ndCode")
    String ndCode;
    @FdDef("conditionNdCode")
    String conditionNdCode;
    @FdDef("resultNdCode")
    String resultNdCode;
    @FdDef("returnNdCode")
    String returnNdCode;
    @FdDef("resultLayoutCode")
    String resultLayoutCode;
    @FdDef("conditionLayoutCode")
    String conditionLayoutCode;
    @FdDef("detailLayoutCode")
    String detailLayoutCode;
    @FdDef("postfix")
    String postfix;
    @FdDef("parentMenuCode")
    String parentMenuCode;
    @FdDef("menuCode")
    String menuCode;
    @FdDef(ndName = "initFdParamNd")
    List<InitFdParam> initFdParams = new ArrayList<>();
    public InitFilterParam(){

    }
    public InitFilterParam(String targetNdCode, String returnNdCode, String postfix){
        this.ndCode = targetNdCode;
        this.returnNdCode = returnNdCode;
        this.postfix = postfix;
        this.initAllNames();
    }

    private void initAllNames(){
        this.resultNdCode = this.ndCode+"RN" + postfix;
        this.conditionNdCode = this.ndCode + "CN" +postfix;
        this.resultLayoutCode = this.ndCode + "RL" + postfix;
        this.conditionLayoutCode = this.ndCode + "CL" +postfix;
        this.menuCode = this.ndCode + "M" + postfix;
        this.detailLayoutCode = this.ndCode + "EL" + postfix;
        this.parentMenuCode = "1-1";
    }

    public void initAllFds(Nd nd){
        Nd returnNd = nd.findNdByNdCode(this.returnNdCode);
        if( returnNd == null)
            throw new RuntimeException(String.format("找不到返回的Nd代码:%s", this.returnNdCode));
        List<String> ndCodes = nd.getAllNdCodes();

        this.initFdByNd(returnNd,true,true);
        for(String ndCode : ndCodes ){
            if (!ndCode.equals(this.returnNdCode))
                initFdByNd(nd.findNdByNdCode(ndCode),false,false);
        }
    }

    private void initFdByNd(Nd nd, boolean isFilter, boolean isResult){
        for( Fd fd : nd.getFds()){
            InitFdParam param = new InitFdParam(nd.getNdCode(),nd.getNdName(),fd.getFdName(),fd.getFdDescription(),isFilter,isResult);
            this.initFdParams.add(param);
        }
    }

    @JsonIgnore
    public List<String> getConditionFdNames(){
        List<String> result = new ArrayList<>();
        for( InitFdParam initFdParam : this.initFdParams){
            if( initFdParam.isFilter())
                result.add(initFdParam.getFdName());
        }
        return result;
    }

    @JsonIgnore
    public List<String> getResultFdNames(){
        List<String> result = new ArrayList<>();
        for( InitFdParam initFdParam : this.initFdParams){
            if( initFdParam.isResult)
                result.add(initFdParam.getFdName());
        }
        return result;
    }
}
